package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.FCDetailBo;
import com.tpyule.tpclass.entity.dto.FavoriteDto;
import com.tpyule.tpclass.entity.pojo.Favorite;
import com.tpyule.tpclass.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/25 14:18
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController extends BaseController {
    @Autowired
    FavoriteService favoriteService;

    @RequestMapping("/add")
    @ResponseBody
    public void addFavorite(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String favorateStr = request.getParameter("favoriteData");

            if(favorateStr!=null) {
                Favorite favorite = objectMapper.readValue(favorateStr, Favorite.class);
                int result = favoriteService.addFavorite(favorite);
                if(result!=0){
                    resultBean = new SuccessResultBean("成功");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public void deleteFavorite(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String favorateStr = request.getParameter("favoriteData");

            if(favorateStr!=null) {
                Favorite favorite = objectMapper.readValue(favorateStr, Favorite.class);
                int result = favoriteService.deleteFavorite(favorite);
                if(result!=0){
                    resultBean = new SuccessResultBean("成功");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

    @RequestMapping("/getSizeFavorite")
    @ResponseBody
    public void getSizeFavorite(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId = request.getParameter("userId");

            if(userId!=null) {
                int result = favoriteService.getSizeFavorite(userId);
                resultBean = new SuccessResultBean(result, "成功");
            } else{
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

    @RequestMapping("/getListFavorite")
    @ResponseBody
    public void getListFavorite(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData = request.getParameter("queryData");

            if(queryData!=null) {
                FavoriteDto dto = objectMapper.readValue(queryData, FavoriteDto.class);
                List<FCDetailBo> fcDetailBos = favoriteService.getListFavorite(dto);
                if(fcDetailBos!=null){
                    resultBean = new SuccessResultBean(fcDetailBos,"成功");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }else{
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }
}
